package report.service.report.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import report.dao.po.ReportDO;
import report.dao.po.ReportTreeDO;
import report.dao.repository.ReportRepository;
import report.dao.repository.ReportTreeRepository;
import report.dao.vo.report.ReportVO;
import report.service.report.IReportService;
import report.service.tree.ITreeService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private ReportRepository reportRepository;
    private ReportTreeRepository reportTreeRepository;
    @Autowired
    // @Qualifier("test")
    private ITreeService treeService;

    /**
     * 创建图表
     * @param pid
     * @param form
     * @return
     */
    public String createReport(String pid, ReportVO form){

        // 在report表中插入数据
        ReportDO reportDO  = new ReportDO();
        reportDO.setPid(pid);
        reportDO.setName(form.getName());
        reportDO.setConfig(form.getConfig());
        reportDO.setDescription(form.getDescription());
        reportDO.setTypeCode(form.getTypeCode());
        reportRepository.save(reportDO);

        // 在report_tree表中插入数据
        treeService.createCata(form.getPid(),form.getName(), form.getTypeCode());
        return "success";
    }

    /**
     * 更新图表
     * @param id
     * @param form
     * @return
     */
    public String updateReport(String id, ReportVO form){

        // 在report表中更新数据
        ReportDO reportDO  = new ReportDO();
        reportDO.setPid(form.getPid());
        reportDO.setId(id);
        reportDO.setName(form.getName());
        reportDO.setConfig(form.getConfig());
        reportDO.setDescription(form.getDescription());
        reportDO.setTypeCode(form.getTypeCode());
        reportRepository.save(reportDO);

        // 在reporttree 表中更新数据
        ReportTreeDO treeVO  = new ReportTreeDO();
        treeVO.setPid(form.getPid());
        treeVO.setName(form.getName());
        treeVO.setId(id);
        treeVO.setTypeCode(form.getTypeCode());

        reportTreeRepository.saveAndFlush(treeVO);

        return "success";
    }

    /**
     * 删除图表
     * @param id
     * @return
     */
    public String deleteReport(String id){
        reportRepository.delete(id);
        reportTreeRepository.delete(id);
        return "success";
    }
}
